<template>
  <div class="sku-list">
    <div class="query">
      <span>商品类型：</span>
      <el-select v-model="queryData.skuStatus" placeholder="请选择" size="medium">
        <el-option label="仅显示停用商品" value="2"></el-option>
        <el-option label="仅显示正常商品" value="1"></el-option>
        <el-option label="显示全部商品" value=""></el-option>
      </el-select>
      <span>关键词：</span>
      <el-input v-model="queryData.goodsNo" placeholder="按商品编号、商品名称等查询" size="medium"></el-input>
      <el-button type="primary" size="medium" @click="query">查询</el-button>
    </div>
    <div class="wrapper">
      <el-tree ref="vuetree" class="tree-wrapper" node-key="id" :data="treeData" :props="defaultProps" highlight-current @node-click="handleNodeClick" default-expand-all></el-tree>
      <div class="table-wrapper">
        <div class="btn-wrapper">
          <el-button type="primary" size="small" @click="$router.push({path:'/skuAdd',query:{kind:queryData.goodsKindFirst}})">新增</el-button>
          <el-button size="small" @click="changeStatus(1)">停用</el-button>
          <el-button size="small" @click="changeStatus(2)">启用</el-button>
          <el-button type="danger" size="small" @click="changeStatus(3)">删除</el-button>
        </div>
        <el-table class="table" :data="tableData" stripe highlight-current-row border @selection-change="changeSelection">
          <el-table-column align="center" type="selection" width="50">
          </el-table-column>
          <el-table-column align="center" type="index" label="序号" width="50">
          </el-table-column>
          <el-table-column align="center" prop="sku_no" label="SKU编号" width="200">
            <template slot-scope="scope">
              <span class="sku-number" @click="$router.push({path:'/skuPrint',query:{sku_nu:scope.row.sku_no}})">{{scope.row.sku_no}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="goods_no" label="商品编号">
          </el-table-column>
          <el-table-column align="center" prop="goods_name" label="商品名称">
          </el-table-column>
          <el-table-column align="center" prop="cate" label="商品类别">
            <template slot-scope="scope">
              <span>{{scope.row.goods_kind_first}}/{{scope.row.goods_kind_second}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="odortype" label="香型">
          </el-table-column>
          <el-table-column align="center" prop="netContent" label="净含量">
          </el-table-column>
          <el-table-column align="center" prop="unit" label="单位">
          </el-table-column>
          <el-table-column align="center" prop="alocohl" label="酒精度">
          </el-table-column>
          <el-table-column align="center" prop="sku_status" label="状态">
          </el-table-column>
          <el-table-column align="center" prop="address" label="操作">
            <template slot-scope="scope">
              <span class="detail-btn" @click="$router.push({path:'/skuDetail',query:{id:scope.row.sku_no}})">查看详情</span>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination v-show='page.total>page.pageSize' background layout="total,prev, pager, next,jumper" :page-size="page.pageSize" :total="page.total" :current-page="page.current" @current-change="handleCurrentChange">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryData: {
        goodsNo: "",
        skuStatus: "",
        goodsKindFirst: ""
      },
      defaultProps: {
        children: "attrKeyList",
        label: "name"
      },
      treeData: [],
      selectData: "",
      tableData: [],
      page: {
        pageSize: 10,
        current: 1,
        total: 0
      }
    };
  },
  mounted() {
    this.getTreeData();
  },
  methods: {
    async getTreeData() {
      let { data } = await this.$post({
        api: "/show/leftmenu"
      });
      this.treeData = data.data;
      this.queryData.goodsKindFirst = data.data[0].attrKeyList[0].id;
      this.getSkuData();
      setTimeout(() => {
        this.$refs.vuetree.setCurrentKey(this.treeData[0].attrKeyList[0].id);
      });
    },
    async getSkuData() {
      let { data } = await this.$post({
        api: "/show/allsku",
        data: {
          current: this.page.current,
          pageSize: this.page.pageSize,
          skuStatus: this.queryData.skuStatus,
          goodsNo: this.queryData.goodsNo,
          goodsKindFirst: this.queryData.goodsKindFirst
        }
      });
      if (data.code === 0) {
        this.tableData = data.data;
        this.page.total = data.rdPage.total;
      } else {
        this.$message({
          type: "error",
          message: data.msg
        });
      }
    },
    query() {
      this.page.current = 1;
      this.getSkuData();
    },
    handleNodeClick(data) {
      this.page.current = 1;
      this.queryData.goodsKindFirst = data.id;
      this.getSkuData();
    },
    handleCurrentChange(val) {
      this.page.current = val;
      this.getSkuData();
    },
    changeSelection(val) {
      let arr = [];
      val.forEach(item => {
        arr.push(item.sku_no);
      });
      this.selectData = arr.join(",");
    },
    changeStatus(type) {
      if (!this.selectData) {
        this.$message({
          message: "请选勾选再进行操作",
          type: "error"
        });
        return;
      }
      let text = type == 1 ? "停用" : type == 2 ? "启用" : "删除";
      let api = type == 1 ? "/end/skustatus" : type == 2 ? "/start/skustatus" : "/delete/skustatus";
      this.$confirm(`确认进行${text}操作吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$post({
          api: api,
          data: {
            skuNos: this.selectData
          }
        }).then(({ data }) => {
          if (data.code === 0) {
            this.getSkuData();
            this.$message({
              type: "success",
              message: `${text}成功`
            });
          } else {
            this.$message({
              type: "error",
              message: data.msg
            });
          }
        });
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消操作"
        });
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.query {
  padding: 15px 0;
  border-bottom: 1px solid #eeeeee;
  span {
    margin-left: 30px;
  }
  .el-input {
    width: 400px;
    margin-right: 30px;
  }
}

.wrapper {
  display: flex;
  min-height: calc(100vh - 171px);
}

.tree-wrapper {
  flex: 0 0 200px;
  width: 200px;
  padding: 15px 0;
  border-right: 1px solid #eeeeee;
}

.table-wrapper {
  flex: 1;
  padding: 15px;
  .table {
    width: 100%;
  }
  .sku-number {
    color: #0033ff;
    text-decoration: underline;
    cursor: pointer;
  }
  .detail-btn {
    color: #0033ff;
    cursor: pointer;
  }
}
</style>